from random import randint
from time import sleep
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner
import unittest

from selenium.webdriver.support.select import Select


class Testbjs(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(2)
        self.driver.get('http://localhost/bajiusi/index.asp')

    def tearDown(self):
        self.driver.quit()

    def test_login(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.assertIn('后台管理系统', self.driver.page_source)
        sleep(2)

    def test_del_unit(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.driver.switch_to.frame('left')
        cd_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(cd_css).click()
        dw_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(dw_css).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_id('chkall').click()
        sc_css = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(sc_css).click()
        self.driver.switch_to.alert.accept()
        self.assertNotIn('name="ID"', self.driver.page_source)
        sleep(2)

    def test_add_unit1(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.driver.switch_to.frame('left')
        cd_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(cd_css).click()
        dl_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(dl_css).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        td_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(td_css).click()
        self.driver.find_element_by_name('bigclass').send_keys('101')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        self.assertNotIn('New Unit', self.driver.page_source)
        sleep(2)

    def test_add_unit2(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.driver.switch_to.frame('left')
        cd_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(cd_css).click()
        xl_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(xl_css).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        td_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(td_css).click()
        sel_lst = Select(self.driver.find_element_by_name('bigclass'))
        lst_count = len(sel_lst.options)
        rand = randint(0, lst_count - 1)
        sel_lst.select_by_index(rand)
        self.driver.find_element_by_name('smallclass').send_keys('102')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        self.assertNotIn('New Unit', self.driver.page_source)
        sleep(2)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(Testbjs('test_login'))
    suite.addTest(Testbjs('test_del_unit'))
    suite.addTest(Testbjs('test_add_unit1'))
    suite.addTest(Testbjs('test_add_unit2'))
    return suite


with open('d:/r.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='进销存测试报告')
    runner.run(test_suite())